/*
 * @Author: 詹真琦(legendryztachi@gmail.com)
 * @Date: 2021-11-05 15:39:31
 * @LastEditors: 白羽(baiyu201803679@qq.com)
 * @LastEditTime: 2022-02-14 15:13:58
 * @FilePath: \意见区d:\seeyon\code\cwp-front\pc-collaboration\src\handles\handle-list\common\print\fragment\postscript.f.js
 * @Description: 附言。入参第一个统一为baseInfo的store
 */
import { createPostscriptList } from '../helper';
import api from '@/api';
function formatDate(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
export default (useBaseInfo, option, printFrom) => {
  let postscriptList = useBaseInfo.print.postscriptData; //这里到时候传一个装最外层附言的数组
  if (printFrom === 'newCol' && useBaseInfo.postscript) {
    postscriptList = [
      {
        content: useBaseInfo.postscript,
        attFileDomain: '',
        assDocDomain: '',
        createDate: formatDate(new Date())
      }
    ];
  }
  if (option && !postscriptList?.length) {
    // 参数组装
    let getCommentParams = () => {
      if (useBaseInfo) {
        var zwIframe = useBaseInfo.baseInfo.zwIframe.mainbody;
        var affair = useBaseInfo.baseInfo.businessData.affair;
        return {
          moduleId: zwIframe.moduleId,
          affairId: affair.id,
          moduleType: zwIframe.moduleType,
          queryType: 2
        };
      }
      return {};
    };
    api.commentGetList(getCommentParams()).then((res) => {
      let { commentData } = res;
      postscriptList = commentData[0]?.comments;
      if (postscriptList) {
        const printPostscriptFragment = new PrintFragment(
          $.i18n('collaboration.sender.postscript'), // 国际化，标题
          createPostscriptList(postscriptList)
        );
        // 这里特殊处理， _doPrint方法里面会调用
        printPostscriptFragment.isCommentFrame = true;
        // 重新请求数据
        option.callback(printPostscriptFragment);
      } else {
        option.callback(null);
      }
    });
  } else {
    if (!postscriptList?.length) return;

    const printPostscriptFragment = new PrintFragment(
      $.i18n('collaboration.sender.postscript'), // 国际化，标题
      createPostscriptList(postscriptList)
    );
    // 这里特殊处理， _doPrint方法里面会调用
    printPostscriptFragment.isCommentFrame = true;
    if (option) {
      option.callback(printPostscriptFragment);
    } else {
      return printPostscriptFragment;
    }
  }
};
